#include "CClient.h"

#define CLIENT_NUMBER   1000
#define THREAD_NUMBER   4

#define CLIENT_AVGNUM   CLIENT_NUMBER / THREAD_NUMBER


BOOL bRun = TRUE;

CClient* TcpClient[CLIENT_NUMBER];

void RecvPacket(int nBegin, int nEnd)
{
    NET_COMMON_PACKET p;
    while (bRun)
    {
        for (int i = nBegin; i < nEnd; i++)
        {
            //TcpClient[i]->Send(&p);
            TcpClient[i]->RunClient();
        }
    }
}

void SendPacket(int ThreadId)
{
    int nBegin = (ThreadId - 1) * CLIENT_AVGNUM;
    int nEnd = ThreadId * CLIENT_AVGNUM;

    for (int i = nBegin; i < nEnd; i++)
    {
        TcpClient[i] = new CClient;
    }

    for (int i = nBegin; i < nEnd; i++)
    {
        //TcpClient[i]->ConnectToServer("192.168.23.146", 4566);
        TcpClient[i]->ConnectToServer("127.0.0.1", 4567);
        //TcpClient[i]->ConnectToServer("182.92.2.52", 4567);
        //TcpClient[i]->ConnectToServer("192.168.31.199", 4567);
        //TcpClient[i]->ConnectToServer("192.168.56.1", 4567);
    }
    printf("<client>%d</client>\r\n", nEnd);

    std::thread t1(RecvPacket, nBegin, nEnd);
    t1.detach();

    std::chrono::milliseconds t(3000);
    std::this_thread::sleep_for(t);

    NET_COMMON_PACKET p;
    while (bRun)
    {
        for (int i = nBegin; i < nEnd; i++)
        {
            TcpClient[i]->Send(&p);
            //TcpClient[i]->RunClient();
            //Sleep(10);
        }
        //Sleep(1000);
    }

    for (int i = nBegin; i < nEnd; i++)
    {
        TcpClient[i]->Close();
    }
}

int main()
{
    for (int i = 0; i < THREAD_NUMBER; i++)
    {
        std::thread t1(SendPacket, i + 1);
        t1.detach();
    }

    getchar();
    return 0;
}